<template>
  <div>
    <el-dialog
      title="编辑"
      :visible="visible"
      width="40%"
      @close="visible = false"
      :close-on-click-modal="false"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-width="100px"
        class="demo-form"
        label-position="right"
        size="mini"
        style="width: 80%"
        v-loading="loading"
      >
        <el-form-item
          label="表单名称"
          prop="title"
          :label-width="defaultLabelWidth"
        >
          <el-input disabled size="mini" type="text" v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item
          label="表单类型"
          prop="formType"
          :label-width="defaultLabelWidth"
        >
          <el-input size="mini" type="text" v-model="form.formType"></el-input>
        </el-form-item>

        <!-- <el-form-item
          label="关联流程"
          :label-width="defaultLabelWidth"
          :rules="rules['workflowId_id']"
        >
          <el-select
            style="width: 100%"
            :value="form.workflowId.id + ':' + form.workflowId.version"
            placeholder="请选择"
            @change="workflowIdChange"
            filterable
            disabled
          >
            <el-option
              v-for="item in allWorkflows"
              :key="item.id + item.activeVersion"
              :label="item.name + ':' + item.activeVersion"
              :value="item.id + ':' + item.activeVersion"
            >
            </el-option>
          </el-select>
        </el-form-item> -->

        <el-form-item label="版本" :label-width="defaultLabelWidth">
          <el-input
            size="mini"
            type="number"
            v-model="form.activeVersion"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="描述"
          prop="description"
          :label-width="defaultLabelWidth"
        >
          <el-input
            size="mini"
            type="text"
            v-model="form.description"
          ></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="confirm" size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      id: 1,
    },
  },
  watch: {
    value: {
      handler() {
        //this.load();
      },
      deep: true,
    },
    visible(newVal, oldVal) {
      if (!newVal && oldVal) this.hide();
    },
  },
  model: {
    prop: "value",
    event: "change",
  },
  data() {
    return {
      loading: false,
      visible: false,
      defaultLabelWidth: "150px",
      form: { workflowId: {}, formDesignId: {} },
      rules: {
        name: [{ required: true, message: "请输入", trigger: "change" }],
      },
      allWorkflows: [],
    };
  },
  methods: {
    confirm() {
      this.commit();
    },
    show(value) {
      this.visible = true;
      // this.getAllWorkflowsWithVersion();
      if (value) this.loadData(value);
    },
    hide() {
      this.visible = false;
      this.form = { workflowId: {}, formDesignId: {} };
    },
    loadData(value) {
      this.loading = true;
      this.$store
        .dispatch("dynamicFormDesign/get", { id: value.formDesignId.id })
        .then((res) => {
          this.form = res;
          this.form.designContent = value.designContent;
          this.form.activeVersion = value.formDesignId.version;
          this.form.designFramework = value.designFramework;
          this.loading = false;
        });
    },
    commit() {
      const _this = this;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.formDesignId = {
            id: this.form.id,
            version: Number(this.form.activeVersion),
          };
          console.log(this.form)
          this.form.workflowId.version = Number(this.form.workflowId.version)
          this.$store
            .dispatch("dynamicFormDesign/update", this.form)
            .then((res) => {
              this.$emit("ok-click", res);
              this.hide();
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getAllWorkflowsWithVersion() {
      this.loading = true
      this.$store
        .dispatch("workflow/getAllWorkflowsWithVersion", this.form)
        .then((res) => {
          this.allWorkflows = res;
          this.loading = false
        });
    },
    workflowIdChange(val) {
      var info = val.split(":");
      this.form.workflowId = { id: info[0], version: info[1] };
    },
  },
};
</script>

<style></style>
